Title: Changing user role after purchasing

Publish Date: Mon, 07 Mar 2016 12:00:29 +0000

Categories: Uncategorized

Content:

Some months ago I talked about how to add a checkout field only for a specific user role with [ablank href="https://www.woothemes.com/woocommerce/"]WooCommerce[/ablank].

Today I'll show you how to change the user role after purchasing a specific product.



Open your functions.php file in wp-content/themes/your-child-theme-name/ and add this code at the end of the file:

https://gist.github.com/27f64a83a2576a966a50

Note the code on line 6. It is a list of products to check, which means that the custom script will check if one of the products purchased by the customer is in that list. If yes, the user role will change to what you define on line 14.

Line 13 removes the customer's old role, by default it's Customer when they buy from you, but it could be different. If you want to remove more than one role, just duplicate the line and change the role slug.

Note: If you defined multiple role switch and the customer purchased more products that will change their role, only the first one found will switch their role.

The original code is by [ablank href="https://gist.github.com/troydean/9322593" nofollow=""]troydean[/ablank].
